---
description: 'Requires all fields to be used at some level by siblings operations.'
---

# `no-unused-fields`

💡 This rule provides
[suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions)

- Category: `Schema`
- Rule name: `@graphql-eslint/no-unused-fields`
- Requires GraphQL Schema: `true`
  [ℹ️](/docs/getting-started#extended-linting-rules-with-graphql-schema)
- Requires GraphQL Operations: `true`
  [ℹ️](/docs/getting-started#extended-linting-rules-with-siblings-operations)

{metadata.description}

## Usage Examples

### Incorrect

```graphql
# eslint @graphql-eslint/no-unused-fields: 'error'

type User {
  id: ID!
  name: String
  someUnusedField: String
}

type Query {
  me: User
}

query {
  me {
    id
    name
  }
}
```

### Correct

```graphql
# eslint @graphql-eslint/no-unused-fields: 'error'

type User {
  id: ID!
  name: String
}

type Query {
  me: User
}

query {
  me {
    id
    name
  }
}
```

### Correct (ignoring fields)

```graphql
# eslint @graphql-eslint/no-unused-fields: ['error', { ignoredFieldSelectors: ['[parent.name.value=PageInfo][name.value=/(endCursor|startCursor|hasNextPage|hasPreviousPage)/]', '[parent.name.value=/Edge$/][name.value=cursor]', '[parent.name.value=/Connection$/][name.value=pageInfo]'] }]

### 1️⃣ YOUR SCHEMA

# Root Query Type
type Query {
  user: User
}

# User Type
type User {
  id: ID!
  name: String!
  friends(first: Int, after: String): FriendConnection!
}

# FriendConnection Type (Relay Connection)
type FriendConnection {
  edges: [FriendEdge]
  pageInfo: PageInfo!
}

# FriendEdge Type
type FriendEdge {
  cursor: String!
  node: Friend!
}

# Friend Type
type Friend {
  id: ID!
  name: String!
}

# PageInfo Type (Relay Pagination)
type PageInfo {
  hasPreviousPage: Boolean!
  hasNextPage: Boolean!
  startCursor: String
  endCursor: String
}

### 2️⃣ YOUR QUERY

query {
  user {
    id
    name
    friends(first: 10) {
      edges {
        node {
          id
          name
        }
      }
    }
  }
}
```

## Config Schema

The schema defines the following properties:

### `ignoredFieldSelectors` (array)

Fields that will be ignored and are allowed to be unused.

E.g. The following selector will ignore all the relay pagination fields for every connection exposed
in the schema:

```json
[
  "[parent.name.value=PageInfo][name.value=/(endCursor|startCursor|hasNextPage|hasPreviousPage)/]",
  "[parent.name.value=/Edge$/][name.value=cursor]",
  "[parent.name.value=/Connection$/][name.value=pageInfo]"
]
```

> [!TIP]
>
> These fields are defined by ESLint
> [`selectors`](https://eslint.org/docs/developer-guide/selectors). Paste or drop code into the
> editor in [ASTExplorer](https://astexplorer.net) and inspect the generated AST to compose your
> selector.

The object is an array with all elements of the type `string`.

Additional restrictions:

- Minimum items: `1`
- Unique items: `true`

## Resources

- [Rule source](https://github.com/dimaMachina/graphql-eslint/tree/master/packages/plugin/src/rules/no-unused-fields.ts)
- [Test source](https://github.com/dimaMachina/graphql-eslint/tree/master/packages/plugin/__tests__/no-unused-fields.spec.ts)
